﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MLM.Data;

namespace MLM.Home
{
    public static class RewriteHelper
    {
        public static string LinkListByCategory(this object categoryName, object categoryID, object NameOfPage)
        {
            string catName = categoryName.ToString();
            if (!string.IsNullOrEmpty(catName))
            {
                return "/" + NameOfPage + "/" + catName.ToSeoName() + "-" + categoryID + ".aspx";
            }
            else
            {
                return "";
            }
        }
        public static string LinkItemDetail(this object catID, object ID, object catName, object Title, object NameOfPage)
        {
            if (!string.IsNullOrEmpty(catName.ToString()))
            {
                string rewrite = "/" + NameOfPage + "/" + catName.ToSeoName() + "/" + Title.ToSeoName();
                if (rewrite.Length > 400)
                {
                    rewrite = rewrite.Substring(400);
                }
                return rewrite + "-" + ID + ".aspx";
            }
            else
            {
                return "";
            }
        }
        public static string LinkItemDetailShort(this object ID, object Title, object NameOfPage)
        {
            return "/" + NameOfPage + "/" + Title.ToSeoName() + "-" + ID + ".aspx";
        }
        public static string LinkItemProductDetail(this object catID, object ID, object catName, object Title, object NameOfPage)
        {
            if (!string.IsNullOrEmpty(catName.ToString()))
            {
                string rewrite = "/" + NameOfPage + "/" + catName.ToSeoName() + "/" + Title.ToSeoName();
                if (rewrite.Length > 400)
                {
                    rewrite = rewrite.Substring(400);
                }
                return rewrite + "-" + ID + ".aspx";
            }
            else
            {
                return "";
            }
        }
        public static string ToSeoName(this object strInput, string seoPattern = "-")
        {
            var strOutput = strInput.ToString().Trim().ToNonUnicode().RemoveSpecialChars().Replace(" ", seoPattern).Replace("----", "-").Replace("---", "-").Replace("--", "-");
            return strOutput;
        }
        public static string ToNonUnicode(this object strInput)
        {
            var strOutput = strInput.ToString().Trim();
            const string originalText = "áàảãạâấầẩẫậăắằẳẵặđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠÂẤẦẨẪẬĂẮẰẲẴẶĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ";
            const string replacedText = "aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYY";
            var index = -1;
            while ((index = strOutput.IndexOfAny(originalText.ToCharArray())) != -1)
            {
                var index2 = originalText.IndexOf(strOutput[index]);
                strOutput = strOutput.Replace(strOutput[index], replacedText[index2]);
            }
            return strOutput;
        }
        public static string RemoveSpecialChars(this object str)
        {
            string[] chars = new string[] { ",", ".", "?", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "-", "_", "(", ")", ":", "|", "[", "]" };
            for (int i = 0; i < chars.Length; i++)
            {
                if (str.ToString().Contains(chars[i]))
                {
                    str = str.ToString().Replace(chars[i], " ");
                }
            }
            return str.ToString();
        }
    }
}